package com.pentofab.payrollmanagement.ui.controller;

import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;

import com.pentofab.payrollmanagement.application.PayrollApplicationFactory;
import com.pentofab.payrollmanagement.dao.LeaveDAO;
import com.pentofab.payrollmanagement.dao.SalaryDAO;
import com.pentofab.payrollmanagement.dao.model.Company;
import com.pentofab.payrollmanagement.dao.model.Holiday;
import com.pentofab.payrollmanagement.dao.model.HolidaysDetail;
import com.pentofab.payrollmanagement.dao.model.Leave;
import com.pentofab.payrollmanagement.dao.model.LeaveDetail;
import com.pentofab.payrollmanagement.dao.model.PTSlab;
import com.pentofab.payrollmanagement.dao.model.PTSlabDetail;
import com.pentofab.payrollmanagement.dao.model.UserAccounts;
import com.pentofab.payrollmanagement.exception.BusinessException;
import com.pentofab.payrollmanagement.ui.control.CustomTextField;
import com.pentofab.payrollmanagement.utilities.PayRollUtility;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;

public class PTSlabController extends AbstractUIController implements
	Initializable, FormValidator{
	
	@FXML
	private TableView<PTSlabDetail> tableView;
	@FXML
    private TableColumn<PTSlabDetail, String> maxRangeCol;
    @FXML
    private TableColumn<PTSlabDetail, String> ptAmountCol;
    @FXML
    private VBox root;
    @FXML
    CustomTextField taxAmount;
    @FXML
    CustomTextField maxNetPay;
    
    SalaryDAO salaryDAO =PayrollApplicationFactory.getSalaryDAO();
	private Company company;
	private UserAccounts userAccounts;
	
	private ArrayList<String> fromMonthDaysList = new ArrayList<String>();

	public void delete(ActionEvent event) {
		
		PTSlabDetail slabDetail = tableView.getSelectionModel().getSelectedItem();
		
		if(slabDetail == null){
			
			showError("Please select a row to delete.");
			return;
		}
		
		if(showConfirmDialog("Are you sure you want to delete selected row.?")){
			 
			try {
				
				salaryDAO.deletePTSlab(slabDetail.getMaxRangeCol());
			
			} catch (BusinessException e) {
				showError(e.getMessage());
				
			}
			refreshUI();
		}
		
	}
	
	/*public void modify(ActionEvent event) {
		
		PTSlabDetail slabDetail = tableView.getSelectionModel().getSelectedItem();
		
		if(slabDetail == null){
			
			showError("Please select a row to delete.");
			return;
		}
		
		if(showConfirmDialog("Are you sure you want to modify selected row.?")){
			
			try {
				
				salaryDAO.deletePTSlab(slabDetail.getMaxRangeCol());
			
			} catch (BusinessException e) {
				showError(e.getMessage());
				
			}
			refreshUI();
		}	
		refreshUI();
	}*/

	public void goBack(ActionEvent event) {

		if (getParent() != null && getParent().length() > 0) {
			PayrollApplicationFactory.getMainController().showScreen(
					getParent(), false);
		}
	
	}
	public void add() {
		
		String validationMsg = getValidationMessage();
		if(!validationMsg.equals("")){
			showError(validationMsg);
			return;
		}
		
		String netPay = maxNetPay.getText().trim();
		String ptAmount = taxAmount.getText().trim();
		
		try{
			
			salaryDAO.addPTSlab(ptAmount, netPay);
			refreshUI();
			showInfo("Successfully added tax slab");
		}catch(Exception e){
			e.printStackTrace();
			showInfo(e.getMessage());
			refreshUI();
		}
		
	}
	public void reset() {
		
		refreshUI(company,userAccounts);
	}
	@Override
	public Parent getView() {
		return root;
	}

	@Override
	public String getTitle() {
		return "PT Slab";
	}
	
	@Override
	public void refreshUI(Object... args) {
		
		if (args != null && args.length > 0) {
			Company company = (Company) args[0];
			this.company = company;
			
			if(args.length > 1 && args[1] instanceof UserAccounts){
				userAccounts = (UserAccounts) args[1];
			}
		}
		
		taxAmount.clear();
		maxNetPay.clear();
		populatePTSlabTable();
	}
	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		
		iniTableProps();
	}

	private void iniTableProps() {

		maxRangeCol.setCellValueFactory(new PropertyValueFactory<PTSlabDetail, String>(
						"maxRangeCol"));
		ptAmountCol.setCellValueFactory(new PropertyValueFactory<PTSlabDetail, String>(
				"ptAmountCol"));
	}
	
	private void populatePTSlabTable() {

		PayRollUtility.debug("===================== populatePTSlabTable start =====================");

		tableView.getItems().clear();
		ObservableList<PTSlabDetail> data = FXCollections.observableArrayList();
		try {
			ArrayList<PTSlab> ptSlab = salaryDAO.getPTSlab();
			System.out.println("ptSlab  : "+ptSlab);
			if(ptSlab == null){
				return;
			}
			data.addAll(PTSlabDetail.getPTSlabDetailList(ptSlab));
		} catch (BusinessException e) {
			e.printStackTrace();
		}
		PayRollUtility.debug("data  : " + data);
		tableView.setItems(data);

		PayRollUtility.debug("===================== populatePTSlabTable end =====================");

	}
	
	@Override
	public String getValidationMessage() {
		
		if(maxNetPay.getText().equals("")){
			return "Max net payto apply PT Slab can not be empty";
		}
		
		if(taxAmount.getText().equals("")){
			return "Max net payto apply PT Slab can not be empty";
		}
		
		return "";
	}

	@Override
	public boolean isValid() {
		return false;
	}
}
